package com.asktgapp.modulebase.common.widget.loading;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.asktgapp.modulebase.R;


/**
 * @author Lee
 * @Title: {标题}
 * @Description:{描述}
 * @date 2016/2/19
 */
public class LoadingDialogFragment extends DialogFragment {
    int mNum;

    /**
     * Create a new instance of MyDialogFragment, providing "num"
     * as an argument.
     */
    public static LoadingDialogFragment newInstance(int num, int layout, String netRequestTag) {
        LoadingDialogFragment f = new LoadingDialogFragment();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        args.putInt("layout", layout);
        args.putString("netRequestTag", netRequestTag);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");

//        // Pick a style based on the num.
//        int style = DialogFragment.STYLE_NORMAL, theme = 0;
//        switch ((mNum - 1) % 6) {
//            case 1:
//                style = DialogFragment.STYLE_NO_TITLE;
//                break;
//            case 2:
//                style = DialogFragment.STYLE_NO_FRAME;
//                break;
//            case 3:
//                style = DialogFragment.STYLE_NO_INPUT;
//                break;
//            case 4:
//                style = DialogFragment.STYLE_NORMAL;
//                break;
//            case 5:
//                style = DialogFragment.STYLE_NORMAL;
//                break;
//            case 6:
//                style = DialogFragment.STYLE_NO_TITLE;
//                break;
//            case 7:
//                style = DialogFragment.STYLE_NO_FRAME;
//                break;
//            case 8:
//                style = DialogFragment.STYLE_NORMAL;
//                break;
//        }
//        switch ((mNum - 1) % 6) {
//            case 4:
//                theme = android.R.style.Theme_Holo;
//                break;
//            case 5:
//                theme = android.R.style.Theme_Holo_Light_Dialog;
//                break;
//            case 6:
//                theme = android.R.style.Theme_Holo_Light;
//                break;
//            case 7:
//                theme = android.R.style.Theme_Holo_Light_Panel;
//                break;
//            case 8:
//                theme = android.R.style.Theme_Holo_Light;
//                break;
//        }
//        setStyle(style, theme);
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        //5.0以下的theme中windowbackground不是透明色  会有边框
//        getDialog().getWindow().setBackgroundDrawableResource(R.color.transparent);
//        setCancelable(false);
//        View v = inflater.inflate(getArguments().getInt("layout"), container, false);
//        v.setBackgroundResource(R.color.app_color_background);
//        return v;
//    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity()) {
            @Override
            public void onBackPressed() {
                dismiss();
            }
        };

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.loading);
//        dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        WindowManager.LayoutParams params = window.getAttributes();
        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        params.width = display.getWidth();
        params.height = display.getHeight();
        params.gravity = Gravity.CENTER;
        params.dimAmount = 0.0f;
        window.setAttributes(params);

        setCancelable(false);
        return dialog;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
//        Communications.cancelRequest(getArguments().getString("netRequestTag"));
    }


}
